<?php
/**
 * 权限
 * @date 2012-3-9
 * @author GhostWei
 */
class system_user_privilege {
	/**
	 * 通过路由来分配权限
	 * @var unknown_type
	 */
	public static $controllerPrivileges = array(
		'index' => 'Home',
		'system' => 'System',
		'supplier' => 'Supplier',
		'shipping' => 'Shipping',
		'payment' => 'Payment',
		'product' => 'Product',
		'promotion' => 'Promotion',
		'order' => 'Order',
		'account' => 'Account',
		'customer' => 'Customer'
	);
	
	private $_isAdministrator = false;
	private $_data = array();
	public function __construct(system_user_row $row) {
		try {
			$privilege = $row->get('privilege');
			$privilege = unserialize($privilege);
		} catch (Exception $e) {
			$privilege = array();
		}
		try {
			$this->_isAdministrator = $row->get('is_administrator');
		} catch (Exception $e) {
		}
		$this->_data = $privilege;
	}
	
	public function isAdministrator() {
		return $this->_isAdministrator;
	}
	
	public function getMenu() {
		if($this->isAdministrator()) {
			return self::$controllerPrivileges;
		}
		if(!isset($this->_data['controller'])) {
			return array();
		}
		$menu = array();
		foreach($this->_data['controller'] as $key => $value) {
			if($value != false) $menu[$key] = $value;
		}		
		return $menu;
	}
	
	public function inController($name) {
		if($this->isAdministrator()) {
			return true;
		}
		if(!isset($this->_data['controller'])) {
			return false;
		}
		$controller = $this->_data['controller'];
		if(isset($controller[$name]) && $controller[$name] != false) {
			return true;
		}
		return false;
	}
	
}